﻿using System;
using System.Data;
using System.Text;
using System.Reflection;
using System.Collections.Generic;
using System.Data.SqlClient;
using ByteSite.DBUtility;
using ByteSite.Common;
using SqlSugar;
using System.Dynamic;
using System.Linq.Expressions;
using System.Linq;
namespace ByteSite.DAL
{
    public class list_item
    {
        public SqlSugarClient db;
        private string databaseprefix; //数据库表名前缀

        public list_item(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
            db = new DBUtility.DbContext().Db;
        }

        public List<Entitys.list_item> GetList(string table,string keyColumn,string valueColumn, Dictionary<string,string> where, string filedOrder="id asc")
        {
            List<Entitys.list_item> list = new List<Entitys.list_item>();
            //list.AddRange(db.Queryable<Entitys.list_item>().AS("dt_form_data_category").OrderBy("id asc").Select("[" + "id" + "] AS [value],[" + "title" + "] AS [key]").ToList());
            if (where != null)
            {
                List<IConditionalModel> conModels = new List<IConditionalModel>();
                foreach (KeyValuePair<string, string> kv in where)
                {
                    conModels.Add(new ConditionalModel { FieldName = kv.Key, ConditionalType = ConditionalType.Equal, FieldValue = kv.Value });
                }
                list.AddRange(db.Queryable<Entitys.list_item>().AS(table).Where(conModels).OrderBy(filedOrder).Select("[" + valueColumn + "] AS [value],[" + keyColumn + "] AS [key]").ToList());
            }
            else
            {
                list.AddRange(db.Queryable<Entitys.list_item>().AS(table).OrderBy(filedOrder).Select("[" + valueColumn + "] AS [value],[" + keyColumn + "] AS [key]").ToList());
            }
            
            return list;
        }

    }
}
